class AccountController < ApplicationController

  def index
    redirect_to(:action => 'login') unless logged_in? || User.count > 0
  end

  def login
    return unless request.post?
    self.current_user = User.authenticate(params[:login], params[:password])
    if logged_in?
      if params[:remember_me] == "1"
        self.current_user.remember_me
        cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
      end
      redirect_back_or_default(:controller => '/home', :action => 'home')
      flash[:notice] = "You are logged in."
    else
      flash[:error] = "Please login again."
    end
  end

  #same as above, but different look
  def studentlogin
	self.login
  end

  def logout
    if not self.current_user.is_not_student?
	is_student = true
    end
    self.current_user.forget_me if logged_in?
    cookies.delete :auth_token
    reset_session
    flash[:notice] = "You are logged out."
    if is_student
        redirect_back_or_default(:controller => '/account', :action => 'studentlogin')
	return
    end
    redirect_back_or_default(:controller => '/home', :action => 'home')
  end
  
end
